<?php include "./pc/view/header.inc.htm"; ?>
<div class="panel">
	<div class="header">注册用户</div>
	<div class="body">
		<div style="width: 300px; margin: auto;">
			<form method="POST" id="create_form" action="user-create.htm" class="form">
				<input type="hidden" name="referer" value="<?php echo $referer; ?>" />
				<p><input type="text" placeholder="Email" name="email" style="width: 100%;"></p>
				<?php if($conf['user_create_email_on']) { ?>
				<p>
					<input type="text" placeholder="验证码" name="verifycode" style="width: 120px;"> 
					<button type="button" loading-text="正在发送..." class="grey" id="sendactive">获取邮件验证码</button> 
				</p>
				<?php } ?>
				<dl class="row margin">
					<dt><button type="submit" loading-text="正在提交..." class="blue big" style="width: 150px;">下一步</button></dt>
					<dd class="right"><a href="user-login.htm">登录</a></dd>
				</dl>
			</form>
		</div>
	</div>
</div>

<?php include "./pc/view/footer.inc.htm"; ?>

<script>

var jform = $('#create_form');
var jemail = jform.find('input[name="email"]').focus();
var jverifycode = jform.find('input[name="verifycode"]');
var jsubmit = jform.find('button[type="submit"]');

$.pdata('referer', '<?php echo $referer; ?>');

jform.off('submit').on('submit', function() {
	jsubmit.button('loading');
	var postdata = jform.serialize();
	$.xpost(jform.attr('action'), postdata, function(code, message) {
		if(code == 0) {
			window.location = 'user-setpw.htm';
			return;
		} else if(code == 1) {
			jemail.popover(message).focus();
		} else if(code == 2) {
			jverifycode.popover(message).focus();
		} else {
			alert(message);
		}
		jsubmit.button('reset');
	});
	return false;
});

var jsendactive = $('#sendactive');
jsendactive.on('click', function() {
	if(!jemail.val()) {
		jemail.popover('请输入 Email').focus();
		return false;
	}
	jsendactive.button('loading');
	$.xpost('user-sendactive.htm', {email: jemail.val()}, function(code, message) {
		if(code == 0) {
			button_count_down(jsendactive, '发送成功 ({time})', 30);
			return;
		} else if(code == 1) {
			jemail.popover(message).focus();
		} else {
			alert(message);
		}
		jsendactive.button('reset');
	});
});

// 倒计时
function button_count_down(jbutton, text, seconds) {
	jbutton.button('disabled');
	if(window.button_count_down_t) clearInterval(window.button_count_down_t);
	window.button_count_down_t = setInterval(function() {
		if(seconds-- <= 0) {
			clearInterval(window.button_count_down_t);
			window.button_count_down_t = 0;
			jbutton.button('reset');
			return;
		}
		text2 = text.replace('{time}', seconds);
		jbutton.button(text2);
	}, 1000);
}

</script>